package com.augurit.common.util.lang;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * 提供集合方面的工具方法
 */
public class CollectionUtils extends org.apache.commons.collections.CollectionUtils {

	/**
	 * 将对象LIST转换为对象数组
	 * @param list
	 * @return 对象数组
	 */
	public static Object[] toObjectArray(List list){
		Object[] objs = null;
		
		if(list != null && list.size() > 0){
			objs = list.toArray(new Object[list.size()]);
		}
		
		return objs;
	}
	
	/**
	 * 将Long对象LIST转换为Long对象数组
	 * @param list
	 * @return Long对象数组
	 */
	public static Long[] toLongArray(List<Long> list){
		Long[] objs = null;
		
		if(list != null && list.size() > 0){
			objs = list.toArray(new Long[list.size()]);
		}
		
		return objs;
	}
	
	/**
	 * 将Long对象SET转换为Long对象数组
	 * @param list
	 * @return Long对象数组
	 */
	public static Long[] toLongArray(Set<Long> set){
		if(set != null && set.size() > 0){
			return set.toArray(new Long[set.size()]);
		}
		return null;
	}
	
	/**
	 * 将String对象LIST转换为String对象数组
	 * @param list
	 * @return String对象数组
	 */
	public static String[] toStringArray(List<String> list){
		String[] objs = null;
		
		if(list != null && list.size() > 0){
			objs = list.toArray(new String[list.size()]);
		}
		
		return objs;
	}
	
	/**
	 * 将String对象LIST转换为Long对象列表
	 * @param strArray
	 * @return String对象数组
	 */
	public static List<Long> toLongList(String[] strArray){
		List<Long> result = new ArrayList();
		
		if(strArray != null && strArray.length > 0){
			for(String str : strArray)
				result.add(Long.valueOf(str));
		}
		
		return result;
	}
	
	/**
	 * 将String对象LIST转换为String对象数组
	 * @param list
	 * @return String对象数组
	 */
	public static String[] toStringArray(Set<String> set){
		String[] objs = null;
		
		if(set != null && set.size() > 0){
			objs = set.toArray(new String[set.size()]);
		}
		
		return objs;
	}
	
	/**
	 * 将Set转换为List
	 * @param set
	 * @return
	 */
	public static List<String> toStringList(Set<String> set){
		List<String> list = null;
		
		if(set != null && set.size() > 0){
			list = new ArrayList();
			list.addAll(set);
		}
		
		return list;
	}
	
	/**
	 * 将array转换为List
	 * @param array
	 * @return
	 */
	public static List<String> toStringList(String[] array){
		List<String> list = null;
		
		if(array != null && array.length > 0){
			list = new ArrayList();
			for(String str : array)
				list.add(str);
		}
		
		return list;
	}
	
	/**
	 * 将Set转换为List
	 * @param set
	 * @return
	 */
	public static List<Long> toLongList(Set<Long> set){
		List<Long> list = null;
		
		if(set != null && set.size() > 0){
			list = new ArrayList();
			list.addAll(set);
		}
		
		return list;
	}
	
	/**
	 * 将字符串的LIST转换为SET
	 * @param strList
	 * @return
	 */
	public static Set<String> convertStringListToSet(List<String> strList){
		Set<String> bufferSet = new HashSet();
		addStringListToSet(bufferSet, strList);
		return bufferSet;
	}
	
	/**
	 * 将LIST中的字符串加入到SET中。适合多个字符串LIST加入SET中的场景。
	 * @param bufferSet
	 * @param strList
	 */
	public static void addStringListToSet(Set<String> bufferSet, List<String> strList){
		if(strList != null && strList.size() > 0){
			for(int i=0; i<strList.size(); i++){
				bufferSet.add(strList.get(i));
			}
		}
	}
	
	/**
	 * 将Long对象的LIST转换为SET
	 * @param longList
	 * @return
	 */
	public static Set<Long> convertLongListToSet(List<Long> longList){
		Set<Long> bufferSet = new HashSet();
		addLongListToSet(bufferSet, longList);
		return bufferSet;
	}
	
	/**
	 * 将LIST中的Long对象加入到SET中。适合多个Long对象LIST加入SET中的场景。
	 * @param bufferSet
	 * @param longList
	 */
	public static void addLongListToSet(Set<Long> bufferSet, List<Long> longList){
		if(longList != null && longList.size() > 0){
			for(int i=0; i<longList.size(); i++){
				bufferSet.add(longList.get(i));
			}
		}
	}
	
	/**
	 * 把数组中的元素加入到list中
	 * @param list
	 * @param array
	 */
	public static void addArrayToList(List<Long> list, Long[] array){
		if(list != null && array != null && array.length > 0){
			for(Long e:array){
				list.add(e);
			}
		}
	}
	
	/**
	 * 判断longObj是否包含在list当中
	 * @param list
	 * @param longObj
	 * @return
	 */
	public static boolean containsAll(List<Long> list, Long... longObj){
		if(list != null && list.size() > 0 && longObj != null){
			for(Long obj : longObj){
				if(!list.contains(obj)){
					return false;
				}
			}
			return true;
		}else
			return false;
	}
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		List<Long> list1 = new ArrayList();
		list1.add(new Long(1));
		list1.add(new Long(2));
		list1.add(new Long(3));
		Long[] arr1 = toLongArray(list1);
		System.out.println(arr1);
		
		List<String> list2 = new ArrayList();
		list2.add("1");
		list2.add("123");
		list2.add("123456");
		String[] arr2 = toStringArray(list2);
		System.out.println(arr2);
		
		
		List<Long> longList = new ArrayList();
		longList.add(new Long(1));
		longList.add(new Long(2));
		longList.add(new Long(3));
		longList.add(new Long(2));
		longList.add(new Long(3));
		Set<Long> set = convertLongListToSet(longList);
		System.out.println(set.size());
	}

}
